package com.asset.salesforce.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.asset.salesforce.model.Appointment;
import com.asset.salesforce.model.Calendar;
import com.asset.salesforce.model.CalendarData;
import com.asset.salesforce.model.Call;
import com.asset.salesforce.model.Lead;
import com.asset.salesforce.service.AppointmentService;
import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.CallService;
import com.asset.salesforce.service.LeadService;

@Controller
public class CalendarController {
	@Autowired
	public CalendarService calendarService;
	
	@Autowired
	public AppointmentService appointmentService;
	
	@Autowired
	public LeadService leadService;
	
	@Autowired
	private CallService callService;
	
	@RequestMapping(value="/getCallCalendar/eventType={eventType}/{parentId}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	@ResponseBody
	public CalendarData getCallCalendar(@PathVariable String eventType,@PathVariable String parentId){
		List<Calendar>calendars = calendarService.getEventByParentId(Integer.parseInt(parentId),eventType);
		CalendarData calendarData = new CalendarData();
		calendarData.setCalendars(calendars);
		return calendarData;
	}
	
	@RequestMapping(value="/getAllAppointmentEventsbyEmpId/eventType={eventType}/{empId}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	@ResponseBody
	public CalendarData getAllAppointmentEventsbyEmpId(@PathVariable String eventType,@PathVariable String empId){
		List<Calendar>calendars2 = new ArrayList<Calendar>();
		List<Calendar>calendars = calendarService.getSelectedEventsbyEmployeeId(eventType, Integer.parseInt(empId));
		for (Calendar calendar : calendars) {
			Appointment appointment = appointmentService.getAppointmentById(new Appointment(), calendar.getParentId());
			Lead lead = leadService.getLeadById(appointment.getParentId());
			if(lead!=null){
				calendars2.add(calendar);
			}
		}
		CalendarData calendarData = new CalendarData();
		calendarData.setCalendars(calendars2);
		return calendarData;
	}
	
	@RequestMapping(value="/getAllCallEventsbyEmpId/eventType={eventType}/{empId}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	@ResponseBody
	public CalendarData getAllCallEventsbyEmpId(@PathVariable String eventType,@PathVariable String empId){
		List<Calendar>calendars2 = new ArrayList<Calendar>();
		List<Calendar>calendars = calendarService.getSelectedEventsbyEmployeeId(eventType, Integer.parseInt(empId));
		for (Calendar calendar : calendars) {
			Call call = callService.getCallnById(new Call(), calendar.getParentId());
			Lead lead = leadService.getLeadById(call.getParentId());
			if(lead!=null){
				calendars2.add(calendar);
			}
		}
		CalendarData calendarData = new CalendarData();
		calendarData.setCalendars(calendars2);
		return calendarData;
	}
}
